﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Net.Mail;


namespace Register.Command
{
    /// <summary>
    /// FileName: MailHelper.cs
    /// CLRVersion: 4.0.30319.269
    /// Author: WangYan
    /// Corporation: 
    /// Description: 
    /// DateTime: 2012/6/25 10:05:43
    /// </summary>
   public class MailHelper
   {
       /// <summary>
       /// 发送电子邮件
       /// </summary>
       /// <param name="Title">电子邮件标题</param>
       /// <param name="Message">电子邮件内容</param>
       /// <param name="Address">收件人地址</param>
       /// <returns>邮件是否发送成功</returns>
       public static  bool SendMail(String Title, String Message,String Address)
       {
           MailParamters paramters = new MailParamters();
           MailMessage message = new MailMessage(new MailAddress(paramters.Useraddress, paramters.Username, Encoding.GetEncoding("UTF-8")), new MailAddress(Address));
           message.Body = Message;
           message.BodyEncoding = Encoding.GetEncoding("utf-8");
           message.IsBodyHtml = true;
           message.Subject = Title;
           message.BodyEncoding = Encoding.GetEncoding("utf-8");
           message.Priority = MailPriority.Normal;

           try
           {
               SmtpClient client = new SmtpClient(paramters.Server, paramters.Port);
               client.EnableSsl = paramters.SSL;
               client.Credentials = new System.Net.NetworkCredential(paramters.Useraddress.Split('@')[0],paramters.Password);
               client.Send(message);
               return true;
           }
           catch (Exception e)
           {
               LogRecordHelper.LogNote("ERROR",e.StackTrace);
               return false;
           }
           
       }

    }

   class MailParamters
   {

       public MailParamters()
       {
           try
           {
               server = ConfigurationManager.AppSettings["Mail_Server"];
               username = ConfigurationManager.AppSettings["Mail_Username"];
               useraddress = ConfigurationManager.AppSettings["Mail_Useraddress"];
               password = ConfigurationManager.AppSettings["Mail_Password"];
               passwordEncrypt = Boolean.Parse(ConfigurationManager.AppSettings["Mail_PasswordEncrypt"]);
               sSL = Boolean.Parse(ConfigurationManager.AppSettings["Mail_SSL"]);
               port = int.Parse(ConfigurationManager.AppSettings["Mail_Port"]);
           }
           catch (Exception e)
           {
               LogRecordHelper.LogNote("ERROR", e.StackTrace);
               throw e;
           }
       }

       private string server;

       public string Server
       {
           get { return server; }
           set { server = value; }
       }
       private string username;

       public string Username
       {
           get { return username; }
           set { username = value; }
       }
       private string useraddress;

       public string Useraddress
       {
           get { return useraddress; }
           set { useraddress = value; }
       }
       private string password;

       public string Password
       {
           get
           {
               if (passwordEncrypt)
                   return DESEncrypt.Decrypt(password);
               else
                   return password;
           }
           set { password = value; }
       }
       private Boolean passwordEncrypt;
       private Boolean sSL;

       public Boolean SSL
       {
           get { return sSL; }
           set { sSL = value; }
       }
       private int port;

       public int Port
       {
           get { return port; }
           set { port = value; }
       }

   }
}
